﻿/*-----------------------------------------------
// File: RoundLeaveState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class RoundLeaveState : Fsm.FsmStateBase<BattleRoundState>
    {
        public enum RoundLeavePhase {
            None,
            RunBack,
            ReadyForLeave,
            Done
        }

        public BattleRoundCtrl ctrl {
            get { return this.controller as BattleRoundCtrl; }
        }
        public RoundLeaveState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) { }
        public RoundLeavePhase currentPhase = RoundLeavePhase.None;

        public override void Enter()
        {
            currentPhase = RoundLeavePhase.RunBack;
        }

        public override void Update()
        {
            if (currentPhase == RoundLeavePhase.RunBack)
            {
                if (isReadyDone())
                {
                    currentPhase = RoundLeavePhase.ReadyForLeave;
                    return;
                }
            }
            else if (currentPhase == RoundLeavePhase.ReadyForLeave)
            {
                if (BattleViewCtrl.GetInstance().roundEndExports.Count > 0)
                {
                    foreach (var export in BattleViewCtrl.GetInstance().roundEndExports)
                    {
                        export.NormalDeal();
                    }
                    BattleViewCtrl.GetInstance().roundEndExports.Clear();
                }
                if (isReady4RoundLeave())
                {
                    BattleViewCtrl.GetInstance().curRunningCamp = nextCamp;
                    currentPhase = RoundLeavePhase.Done;
                    return;
                }
            }
            else if (currentPhase == RoundLeavePhase.Done) {
                ctrl.SwitchState(BattleRoundState.ShowCampRunState);
                return;
            }
        }

        public override void Leave()
        {
            isConfirmLeaved = false;
        }

        bool isReadyDone()
        {
            foreach (var kvp in ctrl.viewCtrl.GetAllActors())
            {
                var bc = kvp.Value;
                if (bc.camp == ctrl.currentCamp)
                {
                    if (bc.GetCurrrentStateType() == ActorState.AttackReady)
                    {

                    }
                    else if (bc.GetCurrrentStateType() == ActorState.Dead)
                    {

                    }
                    else {
                        return false;
                    }
                }
                else {
                    if (bc.GetCurrrentStateType() == ActorState.Defend && bc.IsPlayStand())
                    {

                    }
                    else if (bc.GetCurrrentStateType() == ActorState.Dead)
                    {

                    }
                    else {
                        return false;
                    }
                }
            }
            return true;
        }

        bool isReady4RoundLeave() {
            if (!isConfirmLeaved) {
                return false;
            }
            foreach (var kvp in ctrl.viewCtrl.GetAllActors()) {
                var bc = kvp.Value;
                if (bc.camp == ctrl.currentCamp)
                {
                    if (bc.GetCurrrentStateType() == ActorState.AttackReady)
                    {

                    }
                    else if (bc.GetCurrrentStateType() == ActorState.Dead && !bc.isAlive)
                    {

                    }
                    else {
                        return false;
                    }
                }
                else {
                    if (bc.GetCurrrentStateType() == ActorState.Defend && bc.IsPlayStand())
                    {

                    }
                    else if (bc.GetCurrrentStateType() == ActorState.Dead && !bc.isAlive)
                    {

                    }
                    else {
                        return false;
                    }
                }
            }
            return true;
        }

        bool isConfirmLeaved = false;
        BattleCamp nextCamp;
        public virtual void ConfirmRoundLeave(BattleCamp nextRunCamp) {
            isConfirmLeaved = true;
            nextCamp = nextRunCamp;
        }
    }
}
